package twitter;

import java.io.*;
import java.util.*;

import twitter.model.TwitterConsumerCredentials;
import twitter.model.TwitterHttpResponse;
import twitter.model.TwitterTokenPair;
import twitter.service.TwitterHttpCommand;
import twitter.service.TwitterHttpCommand.HttpMethod;
import twitter.service.TwitterOAuthException;
import twitter.service.TwitterOAuthService;

/**
 * @author Roscas 
 * 
 */
public class Test {

	public static void main(String[] args) {
		Test t = new Test();

		Properties properties = new Properties();
		try {
			properties.load(t.getClass().getResourceAsStream("/twitter.properties"));
		} catch (Exception x) {
			// oops
		}

		String requestTokenUrl = properties.getProperty("twitter.url.request.token");
		String accessTokenUrl = properties.getProperty("twitter.url.access.token");
		String authorizeUrl = properties.getProperty("twitter.url.authorize");
		String consumerKey = properties.getProperty("twitter.consumer.key");
		String consumerSecret = properties.getProperty("twitter.consumer.secret");

		String callbackUrl = "http://localhost:8080/dc/twitterauth";

		TwitterOAuthService service = new TwitterOAuthService(requestTokenUrl, accessTokenUrl, authorizeUrl);

		TwitterConsumerCredentials credentials = new TwitterConsumerCredentials();
		credentials.setKey(consumerKey);
		credentials.setSecret(consumerSecret);

		TwitterTokenPair accessTokens = new TwitterTokenPair();
		
		try {
			// Start the procedure by getting the request token from Twitter
			TwitterTokenPair requestTokens = service.getRequestToken(credentials);
			
			// Build the Authorization URL using the request token
			Map<String, String> callbackParameters = new HashMap<String, String>();
			String authorizationUrl = service.getAuthorizeUrl(requestTokens.getToken(), callbackUrl, callbackParameters);
			System.out.println(authorizationUrl);
			
			// Put a break point here and use the Authorization URL from above to request
			//  access from Twitter to an account
			
			// After you have successfully allowed access on Twitter's site, get the
			//  access token to complete the procedure
			accessTokens = service.getAccessToken(credentials, requestTokens);
			System.out.println(accessTokens);
		} catch (TwitterOAuthException x) {
			System.out.println(x);
		}

		// You can use the access tokens to make requests on behalf of the user.
		//  This one will get the friends of the user
		TwitterHttpResponse response = null;
		TwitterHttpCommand command = new TwitterHttpCommand(credentials);
		try {
			response = service.execute(command, HttpMethod.GET, accessTokens, "http://twitter.com/statuses/friends_timeline.atom", null);
			printInputStream(response.getResponse());
		} catch (TwitterOAuthException ex) {
			System.out.println(ex);
		} catch (IOException ioex) {
			System.out.println(ioex);
		}
	}
	
	public static void printInputStream(InputStream inputStream) throws IOException {
		try {
			ByteArrayOutputStream bao = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			for (int len; 0 < (len = inputStream.read(buffer, 0, buffer.length));) {
				bao.write(buffer, 0, len);
			}
			System.out.println(new String(bao.toByteArray()));
		} finally {
			inputStream.close();
		}
	}

}
